<template>
  <div>
    <a-modal
      :visible.sync="visible"
      v-bind="$attrs"
      v-on="$listeners"
      :maskClosable="false"
      :footer="null"
      width="946px"
      title="审查资料"
      wrapClassName="add-modal-container"
      @cancel="closeModal()"
    >
      <div class="curve-modal-body">
        <a-form-model ref="ruleForm" :model="baseInfo">
          <a-row :gutter="0">
            <a-col :span="24">
              <a-form-model-item label="考核指标">
                <a-input v-model="baseInfo.methods" disabled />
              </a-form-model-item>
            </a-col>
            <a-col :span="24">
              <a-form-model-item label="五级指标">
                <a-input
                  type="textarea"
                  :rows="4"
                  v-model="baseInfo.assessmentContent"
                  disabled
                />
              </a-form-model-item>
            </a-col>
            <a-col :span="24">
              <a-form-model-item label="判定标准">
                <a-input
                  type="textarea"
                  :rows="4"
                  v-model="baseInfo.determine"
                  disabled
                />
              </a-form-model-item>
            </a-col>
          </a-row>
        </a-form-model>
        <vxe-table
          ref="xTable"
          max-height="400"
          size="small"
          align="center"
          border="inner"
          show-overflow
          show-header-overflow
          :data="tableData"
          :column-config="{ resizable: true }"
        >
          <vxe-column field="orgName" title="责任主体"> </vxe-column>
          <vxe-column field="fileName" title="资料名称"> </vxe-column>
          <vxe-column field="userName" title="上传人"> </vxe-column>
          <vxe-column field="createTime" title="上传时间"> </vxe-column>
          <vxe-column
            field="operate"
            width="120"
            title="操作"
            fixed="right"
            align="left"
          >
            <template #default="{ row }">
              <div class="text-button-group">
                <a-button type="link" @click="viewFile(row)"> 预览 </a-button>
                <a-button type="link" @click="downloadFile(row)">
                  下载
                </a-button>
              </div>
            </template>
          </vxe-column>
        </vxe-table>
      </div>
    </a-modal>
    <!-- 附件预览弹窗 -->
    <previewModal
      :visible.sync="previewVisible"
      :loading="previewLoading"
      ref="previewModal"
    ></previewModal>
  </div>
</template>

<script>
import { mapGetters } from "vuex";
import { axios } from "@/utils/request";
import { getToken } from "@/utils/TopVUI";
import { downloadDfs } from "@/utils/TopVUI";
import previewModal from "@/pages/document/minio/previewModal";
export default {
  name: "reviewMaterialModal",
  components: {
    previewModal,
  },
  props: {
    visible: {
      type: Boolean,
      default: false,
    },
    id: {
      type: [String, Number],
      default: "",
    },
  },
  watch: {
    visible: {
      handler(val) {
        this.$emit("update:visible", val);
        if (val) {
          this.getData();
        }
      },
      immediate: true,
    },
  },
  data() {
    return {
      tableData: [],
      baseInfo: {},
      previewVisible: false,
      previewLoading: false,
    };
  },
  computed: {
    ...mapGetters(["configData"]),
    kkFileView() {
      let index = this.configData.findIndex(
        (item) => item.code === "kkFileView"
      );
      if (index >= 0) {
        return this.configData[index].value;
      } else {
        return "http://192.168.1.216:8012/onlinePreview";
      }
    },
  },
  methods: {
    getData() {
      Promise.all([this.getDetail(), this.getTable()])
        .then()
        .finally((_) => {});
    },

    //附件预览
    viewFile(file) {
      let { url } = file;
      this.$refs.previewModal.type = "file";
      this.$refs.previewModal.fileUrl =
        this.kkFileView +
        "/onlinePreview?url=" +
        encodeURIComponent(Base64.encode(url)) +
        "&watermarkTxt=" +
        //水印内容
        encodeURIComponent("") +
        "&token=" +
        getToken();
      this.previewVisible = true;
    },

    //附件下载
    downloadFile(file) {
      let { url } = file;
      if (!url) {
        this.$message.success("文件不存在", 1.5);
        return;
      }
      downloadDfs(url);
    },

    //查询详情
    getDetail() {
      return new Promise((resolve, reject) => {
        axios
          .get(
            `/probusiness/qeTaskDeal/getConfigDetailData?id=${this.id}`,
            {},
            {
              headers: {
                "Content-Type": "application/json",
              },
            }
          )
          .then((res) => {
            this.baseInfo = res?.data?.qeEvaluationContent || {};
            resolve();
          })
          .catch((_) => {
            this.baseInfo = {};
            reject();
          });
      });
    },

    //查询表格
    getTable() {
      return new Promise((resolve, reject) => {
        axios
          .get(
            `/probusiness/qeTaskDeal/getFileRecord?id=${this.id}`,
            {},
            {
              headers: {
                "Content-Type": "application/json",
              },
            }
          )
          .then((res) => {
            this.tableData = res.data || [];
            resolve();
          })
          .catch((_) => {
            this.tableData = [];
            reject();
          });
      });
    },

    //关闭弹窗
    closeModal() {
      this.baseInfo = {};
      this.tableData = [];
      this.$emit("update:visible", false);
    },
  },
};
</script>

<style lang="less" scoped>
@primary-color: @primary-color;
/deep/.add-modal-container {
  .ant-modal-body {
    padding: 0 !important;
  }
  .ant-modal-header {
    position: relative;
    .ant-modal-title {
      &::before {
        content: "";
        position: absolute;
        top: 18px;
        left: 10px;
        background-color: @primary-color;
        height: 17px;
        width: 5px;
      }
    }
  }

  .curve-modal-body {
    padding: 14px;
    max-height: 700px;
    overflow: auto;
  }
  .curve-modal-footer {
    padding: 10px 16px;
    display: flex;
    justify-content: flex-end;
    border-top: 1px solid #e8e8e8;
    border-radius: 0 0 4px 4px;
    button {
      &:last-child {
        margin-left: 20px;
      }
    }
  }
  .ant-form-item {
    display: flex;
    .ant-form-item-label {
      flex-shrink: 0;
      width: 120px;
    }
    .ant-form-item-control-wrapper {
      flex: 1;
    }
  }
}
</style>
